import { createApp } from 'vue';

export const createElement = (string = '<div></div>', autoSetDiv = false) => {
    const parser = new DOMParser();
    // 获取body中的内容
    const el = parser.parseFromString(string, 'text/html').documentElement.querySelector('body').firstChild;

    if (!el) {
        return createElement();
    }

    if (autoSetDiv && el.nodeName == '#text') {
        return createElement(`<div>${string}</div>`);
    }

    return el;
};

export const createVueElement = (App, props) => {
    const el = createElement();
    el.setAttribute('v-cloak', '');
    const app = createApp(App, props);
    app.mount(el);
    return { app, el };
};
